package cn.com.hbhgkj.dao.hibernate;

import java.util.Date;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import cn.com.hbhgkj.bussiness.ReviewCode;
import cn.com.hbhgkj.bussiness.UserType;
import cn.com.hbhgkj.common.DaoException;
import cn.com.hbhgkj.common.Pagination;
import cn.com.hbhgkj.dao.UserDao;
import cn.com.hbhgkj.pojo.User;

public class UserDaoImpl extends GenericDaoImpl<User, Long> implements UserDao {

	public List<User> getAllApplicants() throws DaoException {
		try {
			DetachedCriteria criteria = this.createCriteria();
			criteria.add(Restrictions.eq("userType",
					UserType.APPLICANT.getValue()));
			return this.getHibernateTemplate().findByCriteria(criteria);
		} catch (Exception e) {
			throw new DaoException(e);
		}
	}

	public List<User> findLikeCompanyName(String companyName,
			Pagination pagination) throws DaoException {
		try {
			DetachedCriteria criteria = this.createCriteria();
			criteria.add(Restrictions.like("companyName", companyName,
					MatchMode.ANYWHERE));
			pagination.setTotalCount(this.getTotalCount(criteria));
			return this.getHibernateTemplate().findByCriteria(criteria,
					pagination.getStart(), pagination.getPageSize());
		} catch (Exception e) {
			throw new DaoException(e);
		}
	}

	/*
	 * 功能：通过单位名称或姓名查找用户
	 */
	public List<User> findUsersByName(String name) throws DaoException {
		try {
			DetachedCriteria criteria = this.createCriteria();
			criteria.add(Restrictions.eq("companyName", name));
			return this.getHibernateTemplate().findByCriteria(criteria);
		} catch (Exception e) {
			throw new DaoException(e);
		}
	}

	public List<User> findUsersByUserName(String userName) throws DaoException {
		try {
			DetachedCriteria criteria = this.createCriteria();
			criteria.add(Restrictions.eq("userName", userName));
			return this.getHibernateTemplate().findByCriteria(criteria);
		} catch (Exception e) {
			throw new DaoException(e);
		}
	}

	public List<User> findByReviewCode(ReviewCode reviewCode,
			Pagination pagination) throws DaoException {
		try {

			DetachedCriteria criteria = this.createCriteria();
			criteria.add(Restrictions.eq("status", reviewCode.getValue()));
			pagination.setTotalCount(this.getTotalCount(criteria));
			return this.getHibernateTemplate().findByCriteria(criteria,
					pagination.getStart(), pagination.getPageSize());
		} catch (Exception e) {
			throw new DaoException(e);
		}
	}

	public void updateState(User applicant) throws DaoException {
		try {
			User entity = this.get(applicant.getId());
			entity.setStatus(applicant.getStatus());
			entity.setReviewTime(new Date());
		} catch (Exception e) {
			throw new DaoException(e);
		}
	}

	public List<User> findByReviewCodes(ReviewCode[] reviewCodes,
			Pagination pagination) throws DaoException {
		try {
			DetachedCriteria criteria = this.createCriteria();
			Object[] codes = new Object[reviewCodes.length];
			for (int i = 0; i < reviewCodes.length; i++) {
				codes[i] = reviewCodes[i].getValue();
			}
			criteria.add(Restrictions.in("status", codes));
			// 默认根据注册时间降序排序
			criteria.addOrder(Order.desc("registerTime"));

			pagination.setTotalCount(this.getTotalCount(criteria));
			return this.getHibernateTemplate().findByCriteria(criteria,
					pagination.getStart(), pagination.getPageSize());
		} catch (Exception e) {
			throw new DaoException(e);
		}
	}

}
